//
//  AppManager.swift
//  ShareUmbrella
//
//  Created by niupark on 2017/8/5.
//  Copyright © 2017年 尧尚信息科技. All rights reserved.
//
//  Roger:18684909663

import UIKit
import IQKeyboardManagerSwift
import YSKit
import SWRevealViewController



let APP = AppManager.sharedInstance

class AppManager: YSAppManager {
    
    static let sharedInstance = AppManager()
    fileprivate lazy var mainVC: UIViewController = {
        //配置抽屉式界面
        let mainRevealController = SWRevealViewController(rearViewController:LeftSideVC(), frontViewController: self.homeNavi)!
        
        mainRevealController.rearViewRevealWidth = 80
        mainRevealController.rearViewRevealOverdraw = 120
        mainRevealController.bounceBackOnOverdraw = false
        mainRevealController.stableDragOnOverdraw = true
        mainRevealController.setFrontViewPosition(.right, animated: true)
        return mainRevealController
        
    }()
    
    fileprivate lazy var loginInitVC: UIViewController = {
        //配置抽屉式界面
        let nextStroyBoard = UIStoryboard(name: "Main", bundle: nil)
        let loginInitVC = nextStroyBoard.instantiateInitialViewController()
        return loginInitVC!
        
    }()
    
    private override init() {
        super.init()
        
    }
    
    fileprivate lazy var homeNavi: YSNavigationController? = {
        return YSNavigationController(rootViewController: HomeVC())
    }()
    
    
    /// 构建底部tabbar，
    override func configTab() {
    }
    
    func showMain(){
        self.window.rootViewController = self.mainVC

    }
    
    
    override func configAppearence() {
        super.configAppearence()
        UINavigationBar.appearance().barTintColor = UIColor.clear
        
        self.tab.ys.customize { (vc) in
            vc.tabBar.tintColor = YSColor.c201.color
            vc.tabBar.barTintColor = YSColor.c101.color
        }
        
        //配置界面的统一色调
        YSUI.Apperance.backgroudColor = YSColor.c101.color
        //返回按鈕图片
        YSUI.Apperance.navigationBackImageName = "back"
        YSUI.Apperance.navigationBackImageRect = CGRect(x: 15, y: 12 , width: 12.5, height: 20)
        YSUI.Apperance.navigationTitleColor = UIColor.white
        YSUI.Apperance.navigationBackImageTintColor = UIColor.white
        YSUI.Apperance.navigationBackgroudColor = UIColor.init(red: 65, green: 181, blue: 116)
        YSUI.Apperance.navigationTitleFont = YSFont.t03.font
        YSUI.Apperance.tableViewCellLineColor = YSColor.c104.color
        YSUI.Apperance.tableViewCellBackgroudColor = YSColor.c101.color
        YSUI.Apperance.appConfigSelectedImage = UIImage(named: "勾选对号")
        YSUI.Apperance.tableViewCellArrowImage = UIImage(named: "右键跳转")
        
    }
    
    
    override func configController() {
        super.configController();
        
        //是否登录
        if Auth.isLogin.value {
            self.window.rootViewController = self.mainVC
        }else {
            self.window.rootViewController = self.loginInitVC
        }
        
        
        //        self.window.rootViewController = self.tab
        
        //            // 引导页
        //            let userIsFirstInstalled = UserDefaults.standard.bool(forKey: UserHasGuideView)
        //            if !userIsFirstInstalled {
        //                self.window?.rootViewController = USGuideViewController.init(guideFinished: {
        //                    self.window.rootViewController = self.tab
        //                })
        //            } else if !UserDefaults.standard.bool(forKey: KDidChosenPreferTicker) {
        //                self.window.rootViewController = self.tab
        //                let vc = USPreferStockViewController()
        //                APP.topViewController.sm.present(vc)
        //            } else {
        //                self.window.rootViewController = self.tab
        //            }
        
    }
    
    override func configVariable() {
        //
        super.configVariable()
        
        //高德地图配置
        AMapServices.shared().apiKey = "f65af83282846b43fa3725f3e140bb94"
        AMapServices.shared().enableHTTPS = true

        //监听用户登录操作
        _  = Auth.isLogin.asObservable().takeUntil(self.rx.deallocated).subscribe(onNext: { [weak self] (isLogin) in
            guard let `self` = self else {
                return
            }
            if isLogin {
                self.window.rootViewController = self.mainVC
            }else {
                self.window.rootViewController = self.loginInitVC
            }
            
        })
        
    }
    
    
}

